package com.cman777.springc.sample.controller;

import com.cman777.springc.common.bean.exception.BusinessException;
import com.cman777.springc.common.bean.response.JsonResult;
import com.cman777.springc.idempotence.annotation.IdempotenceId;
import com.cman777.springc.sample.bean.dto.request.TestExportRequest;
import com.cman777.springc.sample.bean.dto.request.TestCrudRequest;
import com.cman777.springc.sample.bean.dto.request.TestRequest;
import com.cman777.springc.sample.bean.dto.response.TestExportResponse;
import com.cman777.springc.sample.bean.dto.request.TestPageRequest;
import com.cman777.springc.sample.bean.dto.response.TestPageResponse;
import com.cman777.springc.sample.bean.dto.response.TestResponse;
import com.cman777.springc.sample.facade.TestFacade;
import com.cman777.springc.poi.annotation.ExlExport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @author chenzhicong
 * @time 2020/3/17 0:34
 * @description
 */
@Api(tags = "演示控制器")
@RestController
@RequestMapping("/test")
@Log4j2
public class TestController {
    @Autowired
    private TestFacade testFacade;


    @ApiOperation(value = "增删改查")
    @GetMapping("/crud")
    public JsonResult testCrud(@Valid TestCrudRequest request){
        log.info(request.toString());
        return new JsonResult();
    }


    @ApiOperation(value = "分页+导出")
    @GetMapping("/page")
    @ExlExport("测试导出名称")
    public JsonResult<TestPageResponse> testPage(@Valid TestPageRequest request){
        return new JsonResult<TestPageResponse>().content(testFacade.testPage(request));
    }

    @ApiOperation(value = "缓存接口")
    @GetMapping("/cache")
    public JsonResult<TestResponse> testCache(@Valid TestRequest request){
        return new JsonResult<TestResponse>().content(testFacade.testCache(request));
    }

    @ApiOperation(value = "testES")
    @GetMapping("/testEs")
    //@IdempotenceId(salt = "#salt",notDeleteForException = BusinessException.class)
    public JsonResult testES(String salt) throws IOException {
        testFacade.testES();
        if(Integer.valueOf(salt)> 100){
            throw new BusinessException("123");
        }
        return new JsonResult();
    }





}
